package com.my.hp_hr.contorller.liuhao_controller;

import com.my.hp_hr.entity.liuhao_entity.Employee;
import com.my.hp_hr.entity.liuhao_entity.Leave;
import com.my.hp_hr.service.liuhao_service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/employeeLeave")
public class EmployeeLeaveAddServlet extends HttpServlet {
    public LeaveService leaveService=new LeaveService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String start_time_str = req.getParameter("start");
            String end_time_str = req.getParameter("end");
            String days = req.getParameter("days");
            String type = req.getParameter("type");
            String reason = req.getParameter("reason");
            Employee esl= (Employee) req.getSession().getAttribute("employee_isLogin");
            Date start_time = sdf.parse(start_time_str);
            Date end_time = sdf.parse(end_time_str);
            Leave leave = new Leave(0, esl.getEmployee_number(), esl.getDepartment_number(), start_time, end_time, days, reason, type, null, "未批准", null,esl);
            leaveService.addEmployeeLeave(leave);
            req.getRequestDispatcher("oneself_leaveList").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
